﻿using System;

public class NameTable
{
    internal short[] attrib;
    internal int height;
    internal string name;
    internal short[] tile;
    internal int width;

    public NameTable(int width, int height, string name)
    {
        this.name = name;
        this.width = width;
        this.height = height;
        this.tile = new short[width * height];
        this.attrib = new short[width * height];
    }

    public virtual short getAttrib(int x, int y)
    {
        return this.attrib[(y * this.width) + x];
    }

    public virtual short getTileIndex(int x, int y)
    {
        return this.tile[(y * this.width) + x];
    }

    public virtual void stateLoad(ByteBuffer buf)
    {
        for (int i = 0; i < (this.width * this.height); i++)
        {
            this.tile[i] = buf.readShort();
        }
        for (int j = 0; j < (this.width * this.height); j++)
        {
            this.attrib[j] = buf.readShort();
        }
    }

    public virtual void stateSave(ByteBuffer buf)
    {
        for (int i = 0; i < (this.width * this.height); i++)
        {
            buf.putShort(this.tile[i]);
        }
        for (int j = 0; j < (this.width * this.height); j++)
        {
            buf.putShort(this.attrib[j]);
        }
    }

    public virtual void writeAttrib(int index, int value_Renamed)
    {
        int num = index % 8;
        int num2 = index / 8;
        num *= 4;
        num2 *= 4;
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                int num3 = (value_Renamed >> (2 * ((i * 2) + j))) & 3;
                for (int k = 0; k < 2; k++)
                {
                    for (int m = 0; m < 2; m++)
                    {
                        int num4 = (num + (j * 2)) + m;
                        int num5 = (num2 + (i * 2)) + k;
                        this.attrib[(num5 * this.width) + num4] = (short) ((num3 << 2) & 12);
                    }
                }
            }
        }
    }

    public virtual void writeTileIndex(int index, int value_Renamed)
    {
        this.tile[index] = (short) value_Renamed;
    }
}

